﻿@model SalePromotion.Models.EBayListingModel
@using (@Html.BeginForm("ChooseListing", "Home"))
{
    <div class="search">
        <span></span><span>Sort by</span>
        @Html.HiddenFor(m => m.Id)
        @Html.DropDownListFor(i => i.SortBy, Model.SortBys, new { placeholder = "Sort by" })
        <span>Listing Format</span>
        @Html.DropDownListFor(i => i.ListingFormat, Model.ListingFormats, new { placeholder = "Listing Format" })
        <input type="submit" value="Search" class="btn" />
    </div>
}
@if (Model.HasItems)
{
    using (@Html.BeginForm("ConfirmChooseListing", "Home", FormMethod.Post, new { id = "listingForm" }))
    {
    @Html.HiddenFor(m => m.Id)
    <span>@Model.EBayListings.Count() Items Found.@Model.SelectedCount Is(are) Selected.</span>
    <table class="zebra-striped">
        <thead>
            <tr>
                <th style="width: 40px">
                    <input type="checkbox" name="ckbAll" id="ckbAll" />
                </th>
                <th>
                    Picture
                </th>
                <th>
                    Item#
                </th>
                <th>
                    Current Price
                </th>
                <th>
                    Qty
                </th>
                <th>
                    Shipping
                </th>
                <th>
                    Item Format
                </th>
                <th>
                    (PST) End Date
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.EBayListings)
            {
                <tr>
                    <td>
                        @*        @Html.CheckBoxFor(m => item.IsSelected)*@
                        @Html.CheckBox("item.IsSelected", item.IsSelected)
                        @Html.HiddenFor(m => item.ItemId)
                    </td>
                    <td>
                        <a>
                            <img src="@item.PicitureURL" alt="@item.Title"/></a>
                    </td>
                    <td>
                        <a href='http://cgi.sandbox.ebay.com/ws/eBayISAPI.dll?ViewItem&item=@item.ItemId&category='>@item.Title</a><br />@item.ItemId
                    </td>
                    <td>@item.CurrentPrice
                    </td>
                    <td>@item.Qty
                    </td>
                    <td>@item.ShippingCost
                    </td>
                    <td>
                        <a>
                            <img src="@Url.Content(item.ItemFormatImageUrl)" /></a>
                    </td>
                    <td>@item.EndDate
                    </td>
                </tr>
            }
        </tbody>
    </table>          
    <input type="submit" value="Choose" class="btn" />
    @Html.ActionLink("Cancel", "SaleEdit", new { Id = Model.Id, fromSession =true })
    }
}
else
{
    <p>
        <em>There are no Listings.</em></p>
}
@section Scripts{
    <script type="text/javascript">

        $(function () {

            //        $("form#listingForm").submit(function (e) {
            //            var valid = false; ;
            //            var selectedLength = $("input[name='item.IsSelected'][checked='true']").length;
            //            $("input[name='item.IsSelected']").each(function () {
            //                if ($(this).attr("checked") == "true") {
            //                    valid = true;
            //                    return ;
            //                }
            //            });
            //            if (!valid) {
            //                alert("please choose at least one listing.");
            //                e.preventDefault();
            //                return false;
            //            }
            //        });


            $("#ckbAll").click(function () {
                if ($(this).attr("checked") == true) {
                    $("input[name='item.IsSelected']").each(function () {
                        $(this).attr("checked", "true");
                    });

                } else {
                    {
                        $("input[name='item.IsSelected']").each(function () {
                            $(this).attr("checked", "");
                        });
                    }
                }
            });

        });
    </script>
}
